package per.dg.digital_klotski;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.List;

public class DigitalKlotskiView extends View {

    private Context mContext;

    private List chessPieces;

    private int minSpac = 100;

    private int spac = 3;

    public void setSpac(int spac) {
        this.spac = spac;
    }

    public DigitalKlotskiView(Context context) {
        this(context, null);
    }

    public DigitalKlotskiView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DigitalKlotskiView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initData(context);
    }

    private void initData(Context context) {
        mContext = context;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measure(widthMeasureSpec);
        int height = measure(heightMeasureSpec);
        width = height = width > height ? height : width;
        setMeasuredDimension(width, height);
    }

    private int measure(int measureSpace){
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpace);
        int specSize = MeasureSpec.getSize(measureSpace);
        if(specMode == MeasureSpec.EXACTLY) {
            //精确值模式
            result = specSize;
        }else if(specMode == MeasureSpec.AT_MOST) {
            //最大值模式
            result = minSpac;
            result = Math.min(result,specSize);
        }

        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(mContext.getResources().getColor(R.color.colorAccent));

        Paint paint = new Paint();
        paint.setColor(mContext.getResources().getColor(R.color.colorPrimary));
        canvas.drawRect(0, 0, getWidth()/spac, getHeight()/spac, paint);
    }
}
